﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HugeBear : Ship
{
    float m_CurrentShockRange = 0;
    float m_ShockRange = 16;
    float m_ShockDensity = 200;
    bool m_Start = false;
    public CollsionCallback m_CollsionCallback;
    public AudioSource m_SkillAudio;
    float m_FlickTiemCount = 0;
    List<GameObject> m_FlickObjs = new List<GameObject>();
    protected override void Awake()
    {
        base.Awake();
        m_CollsionCallback.OnTrigger2DEnterCallback = OnCollsionCallback;
        m_CollsionCallback.OnTrigger2DExitCallback = OnCollsionExitCallback;
    }
    protected override void Update()
    {
        base.Update();
        UpdateSkill();
    }

    void UpdateSkill()
    {
        if (m_Start)
        {
            m_CurrentShockRange += Time.deltaTime * Mathf.Clamp(m_ShockRange*10 - m_CurrentShockRange*10, 5, m_ShockRange);
            if (m_CurrentShockRange > m_ShockRange)
            {
                m_Start = false;
            }
            m_CollsionCallback.gameObject.transform.localScale = new Vector3(m_CurrentShockRange, m_CurrentShockRange, m_CurrentShockRange);
            
        }
        else
        {
            m_CurrentShockRange -= Time.deltaTime * 2;
            m_CollsionCallback.gameObject.transform.localScale = new Vector3(m_CurrentShockRange, m_CurrentShockRange, m_CurrentShockRange);
        }

        //  技能时，将东西弹开
        if (m_CollsionCallback.gameObject.activeSelf)
        {
            m_FlickTiemCount += Time.deltaTime;
            if (m_FlickTiemCount > 0.3f)
            {
                m_FlickTiemCount = 0;
                foreach (var obj in m_FlickObjs)
                {
                    if (!obj)
                        continue;
                    var shipTarget = obj.GetComponent<Ship>();
                    if (shipTarget)
                    {
                        var diffenceVector = shipTarget.transform.position - transform.position;
                        float diffence = m_ShockRange - diffenceVector.magnitude;
                        if (shipTarget && diffence > 0)
                        {
                            float force = (diffence / m_ShockRange) * m_ShockDensity;
                            shipTarget.AddForce(diffenceVector, force);
                        }
                    }
                    var flyItemTarget = obj.GetComponent<FlyItem>();
                    if (flyItemTarget)
                    {
                        var diffenceVector = flyItemTarget.transform.position - transform.position;
                        flyItemTarget.FlyDirection = diffenceVector.normalized;
                        flyItemTarget.m_Ship = this.gameObject;
                    }
                }
            }
        }
        if (m_CurrentShockRange < 0 && m_CollsionCallback.gameObject.activeSelf)
        {
            m_CollsionCallback.gameObject.SetActive(false);
            m_FlickObjs.Clear();
        }
    }
    public override bool LaunchSkillInClient()
    {
        return base.LaunchSkillInClient();
    }
    public override bool LaunchSkill()
    {
        if (!base.LaunchSkill())
            return false;
        m_Start = true;
        m_CurrentShockRange = 0;
        m_CollsionCallback.gameObject.transform.localScale = new Vector3(1,1,1);
        m_CollsionCallback.gameObject.SetActive(true);
        m_SkillAudio.Play();
        m_FlickTiemCount = 1;
        return true;
    }
    void OnCollsionCallback(Collider2D value)
    {
        if(UtilsGame.IsOnline() && !UtilsGame.IsServer(this.gameObject))
        {
            return;
        }
        var shipTarget = value.gameObject.GetComponent<Ship>();
        var flyItemTarget = value.gameObject.GetComponent<FlyItem>();
        if ((shipTarget && value.gameObject != gameObject) || 
            flyItemTarget)
        {
            m_FlickObjs.Add(value.gameObject);
        }



    }
    void OnCollsionExitCallback(Collider2D value)
    {
        if (UtilsGame.IsOnline() && !UtilsGame.IsServer(this.gameObject))
        {
            return;
        }
        m_FlickObjs.Remove(value.gameObject);


    }
    public override void OnChase()
    {
        if (SkillIsReady)
            LaunchSkill();
    }
    public override void OnFight()
    {
        if (SkillIsReady)
            LaunchSkill();
    }
    public override void OnRetreat()
    {
        if (SkillIsReady)
            LaunchSkill();
    }
}
